package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/*套餐菜品类*/
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired(required = false)
    private SetmealService setmealService;

    /*保存新增套餐菜品信息*/
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session) {
        //获取并补全登入者信息
        Long empId = (Long) session.getAttribute("employee");
        setmealDto.setUpdateUser(empId);
        setmealDto.setCreateUser(empId);
        setmealService.save(setmealDto);
        return R.success("保存成功");
    }
    /**
     *套餐分页查询*/
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page,Integer pageSize,String name){
      Page<SetmealDto> pageResult =  setmealService.findByPage(page,pageSize,name);
      return R.success(pageResult);
    }
    /*删除套餐*/
    @DeleteMapping
    public R<String> Delete(@RequestParam List<Long> ids){
        setmealService.DeleteByIds(ids);
        return R.success("删除成功");
    }
    /**
     * 根据套餐的类别展示套餐
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(long categoryId,Integer status){
       List<Setmeal> setmealList = setmealService.list(categoryId,status);
       return R.success(setmealList);
    }
    /*套餐的批量起售，停售*/
    @PostMapping("/status/{status}")
    public R<String> statusByIds(@RequestParam List<Long> ids,@PathVariable Integer status){
        setmealService.statusByIds(ids,status);
        return R.success("修改状态成功");
    }
   // 修改套餐
    @PutMapping
    public R<String> UpDateSetmeal(@RequestBody SetmealDto setmealDto,HttpSession session){
        //修改当前登入者修改时间
        Long empId = (Long) session.getAttribute("employee");
        setmealDto.setUpdateUser(empId);

        setmealService.UpDateSetmealById(setmealDto);
        return R.success("修改成功");
    }
    /*回显数据*/
    @GetMapping("/{id}")
    public R<SetmealDto> findBySetmealId(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.findBySetmealId(id);
        return R.success(setmealDto);
    }
}
